package com.struts2.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.hibernate.HibernateUtil;
import com.struts2.model.Category;
import com.struts2.model.Color;
import com.struts2.model.Designer;
import com.struts2.model.Gender;
import com.struts2.model.Product_item;
import com.struts2.model.Product_line;
import com.struts2.model.Season;
import com.struts2.model.Size;
import com.struts2.model.Type;

public class ProductUploadService {
/*	
	public List<String> getGender(List<String> gender) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		String hql = "SELECT g.name FROM Gender g";
		
		Query query = session.createQuery(hql);

		session.getTransaction().commit();
		gender = query.list();
		session.close();
		sF.close();
		return gender;
	}
	
	public List<String> getCategory(List<String> category) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		String hql = "SELECT c.name FROM Category c";
		//String hql = "SELECT c.name FROM Category c Where c.id IN (SELECT gc.category_id FROM gender_category gc WHERE gc.gender_id = (Select g.id FROM gender g where g.name ='"+genderselected+"'))";
		
		Query query = session.createQuery(hql);

		session.getTransaction().commit();
		category = query.list();
		session.close();
		sF.close();
		return category;
	}
	
	public List<String> getType(List<String> type) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		String hql = "SELECT t.name FROM Type t";
		//String hql = "SELECT c.name FROM Category c Where c.id IN (SELECT gc.category_id FROM gender_category gc WHERE gc.gender_id = (Select g.id FROM gender g where g.name ='"+genderselected+"'))";
		
		Query query = session.createQuery(hql);

		session.getTransaction().commit();
		type = query.list();
		session.close();
		sF.close();
		return type;
	}
	
	public List<String> getSeason(List<String> season) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		String hql = "SELECT s.name FROM Season s";
		//String hql = "SELECT c.name FROM Category c Where c.id IN (SELECT gc.category_id FROM gender_category gc WHERE gc.gender_id = (Select g.id FROM gender g where g.name ='"+genderselected+"'))";
		
		Query query = session.createQuery(hql);

		session.getTransaction().commit();
		season = query.list();
		session.close();
		sF.close();
		return season;
	}
	*/
	public Gender genderModel(Gender genderModel, String genderselected){
		System.out.println("genderModel called");
		System.out.println(genderselected);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Gender.class,
				"gender");
		criteria.add(Restrictions.eq("gender.name", genderselected));
		genderModel = (Gender) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(genderModel);
		return genderModel;
	}
	
	public Category categoryModel(Category categoryModel, String categoryselected){
		System.out.println("categoryModel called");
		System.out.println(categoryselected);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Category.class,
				"category");
		criteria.add(Restrictions.eq("category.name", categoryselected));
		categoryModel = (Category) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(categoryModel);
		return categoryModel;
	}
	
	public Type typeModel(Type typeModel, String typeselected){
		System.out.println("typeModel called");
		System.out.println(typeselected);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Type.class,
				"type");
		criteria.add(Restrictions.eq("type.name", typeselected));
		typeModel = (Type) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(typeModel);
		return typeModel;
	}
	
	public Season seasonModel(Season seasonModel, String seasonselected){
		System.out.println("seasonModel called");
		System.out.println(seasonselected);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Season.class,
				"season");
		criteria.add(Restrictions.eq("season.name", seasonselected));
		seasonModel = (Season) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(seasonModel);
		return seasonModel;
	}
	
	public Designer designerModel(Designer designer, String designername){
		System.out.println("designerModel called");
		System.out.println(designername);
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Designer.class,
				"designer");
		criteria.add(Restrictions.eq("designer.username", designername));
		designer = (Designer) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		sF.close();
		System.out.println(designer);
		return designer;
	}
	
	public List<Color> getColor(List<Color> color) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Color.class,
				"color");
		
		color = (List<Color>) criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return color;
	}
	
	public int getSizeNo(int sizeNo) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Size.class,
				"size");
		
		sizeNo = (int) criteria.list().size();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return sizeNo;
	}
	
	public int getColorNo(int colorNo) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Color.class,
				"color");
		
		colorNo = (int) criteria.list().size();

		session.getTransaction().commit();
		session.close();
		sF.close();
		return colorNo;
	}
	
	public Product_line insert(Product_line productLine, Gender genderModel, Category categoryModel, Type typeModel, Season seasonModel, Designer designer, String name, String description, int price){
		System.out.println("insert called");
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		System.out.println(dateFormat.format(date));
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		session.beginTransaction();
		productLine.setGender(genderModel);
		productLine.setCategory(categoryModel);
		productLine.setType(typeModel);
		productLine.setSeason(seasonModel);
		productLine.setDesigner(designer);
		productLine.setName(name);
		productLine.setDescription(description);
		productLine.setPrice(price);
		productLine.setDate(date);
		session.save(productLine);
		session.getTransaction().commit();
		session.close();
		sF.close();
		return productLine;
	}
	
}